package com.springboot.uav.controller;

import com.springboot.uav.dto.MyOrdersQueryDto;
import com.springboot.uav.dto.OrderQueryDto;
import com.springboot.uav.dto.OrdersDto;
import com.springboot.uav.entity.Result;
import com.springboot.uav.service.OrdersService;
import com.springboot.uav.util.JWTUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.ExampleObject;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import net.sourceforge.tess4j.TesseractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/orders")
@Slf4j
@Tag(name = "订单接口", description = "订单管理相关接口")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private JWTUtil jwtUtil;

    @PostMapping("/add/getReward")
    @Operation(summary = "发布订单获取报酬接口", description = "通过输入的参数进行计算金额")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "下单成功"),
            @ApiResponse(responseCode = "400", description = "未填写相关信息"),
            @ApiResponse(responseCode = "1001", description = "取货地址为空"),
            @ApiResponse(responseCode = "1002", description = "收件地址为空"),
            @ApiResponse(responseCode = "1003", description = "联系方式为空"),
            @ApiResponse(responseCode = "1004", description = "包裹数量为空"),
            @ApiResponse(responseCode = "1005", description = "取件码为空"),
            @ApiResponse(responseCode = "1006", description = "取件码数量和包裹数量不一致"),
            @ApiResponse(responseCode = "1007", description = "余额不足"),
    })
    public Result getReward(@RequestBody OrdersDto ordersDto) {
        return ordersService.getReward(ordersDto);
    }

    @PostMapping("/add/success")
    @Operation(summary = "确认下单接口", description = "看见报酬后选择确认下单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "下单成功"),
            @ApiResponse(responseCode = "400", description = "超时1分钟")
    })
    public Result addOrder(@RequestBody OrdersDto ordersDto) {
        return ordersService.addOrder(ordersDto);
    }

    @PostMapping("/query")
    @Operation(summary = "查询订单接口", description = "接收前端的请求进行筛选分页查询返回")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 200,
                                        "msg": "查询成功",
                                        "data": {
                                            "orders": [
                                                {
                                                    "orderId": 314,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑0925",
                                                    "smallNum": 0,
                                                    "mediumNum": 1,
                                                    "bigNum": 0,
                                                    "reward": 2,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 313,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1138",
                                                    "smallNum": 1,
                                                    "mediumNum": 1,
                                                    "bigNum": 1,
                                                    "reward": 6,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 307,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑0544",
                                                    "smallNum": 0,
                                                    "mediumNum": 0,
                                                    "bigNum": 1,
                                                    "reward": 3,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 299,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1409",
                                                    "smallNum": 0,
                                                    "mediumNum": 0,
                                                    "bigNum": 0,
                                                    "reward": 0,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 290,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1346",
                                                    "smallNum": 0,
                                                    "mediumNum": 1,
                                                    "bigNum": 1,
                                                    "reward": 5,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 287,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1506",
                                                    "smallNum": 1,
                                                    "mediumNum": 0,
                                                    "bigNum": 1,
                                                    "reward": 4,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 283,
                                                    "customerId": 1,
                                                    "timeAgo": "16小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1405",
                                                    "smallNum": 1,
                                                    "mediumNum": 1,
                                                    "bigNum": 0,
                                                    "reward": 3,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 272,
                                                    "customerId": 1,
                                                    "timeAgo": "17小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑1001",
                                                    "smallNum": 0,
                                                    "mediumNum": 1,
                                                    "bigNum": 1,
                                                    "reward": 5,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 268,
                                                    "customerId": 1,
                                                    "timeAgo": "17小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑0622",
                                                    "smallNum": 1,
                                                    "mediumNum": 1,
                                                    "bigNum": 1,
                                                    "reward": 6,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                },
                                                {
                                                    "orderId": 267,
                                                    "customerId": 1,
                                                    "timeAgo": "17小时前",
                                                    "description": "此订单做测试用",
                                                    "pickupAddress": "深大粤海菜鸟驿站",
                                                    "deliveryAddress": "冬筑0250",
                                                    "smallNum": 0,
                                                    "mediumNum": 1,
                                                    "bigNum": 1,
                                                    "reward": 5,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/172fb1f2-990f-4a5c-ab58-82fabe667c7d.jfif",
                                                    "nickname": "香菇"
                                                }
                                            ],
                                            "totalCount": 44,
                                            "totalPages": 5
                                        }
                                    }"""))),
    })
    public Result ordersFind(@RequestBody OrderQueryDto orderQueryDto) {
        return ordersService.orderQuery(orderQueryDto);
    }

    @PostMapping("/myQuery")
    @Operation(summary = "查询我的订单接口", description = "接收前端的请求进行筛选分页查询返回")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 200,
                                        "msg": "查询成功",
                                        "data": {
                                            "orders": [
                                                {
                                                    "orderId": 149,
                                                    "customerId": 10,
                                                    "courierId": 1,
                                                    "timeAgo": "1天前",
                                                    "description": "",
                                                    "pickupAddress": "深大沧海菜鸟驿站",
                                                    "deliveryAddress": "冬筑798",
                                                    "smallNum": 1,
                                                    "mediumNum": 1,
                                                    "bigNum": 0,
                                                    "reward": 4,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/d533d29a-ad0f-484d-b87c-83ac6074a3cb.jpg",
                                                    "nickname": "wwww",
                                                    "phoneNumber": "15017217711",
                                                    "code": "13-3-701,42-3-2004",
                                                    "orderStatus": 1
                                                },
                                                {
                                                    "orderId": 145,
                                                    "customerId": 8,
                                                    "courierId": 1,
                                                    "timeAgo": "14天前",
                                                    "description": "",
                                                    "pickupAddress": "深大冬筑1326",
                                                    "deliveryAddress": "1234",
                                                    "smallNum": 1,
                                                    "mediumNum": 1,
                                                    "bigNum": 0,
                                                    "reward": 4,
                                                    "avatar": "https://web-llxx.oss-cn-shenzhen.aliyuncs.com/12595d80-4dbd-4041-9d37-9e0bba5fc39b.jpg",
                                                    "nickname": "小菜鸡",
                                                    "phoneNumber": "19314325316",
                                                    "code": "13-3-7011,42-3-2004",
                                                    "orderStatus": 1
                                                }
                                            ],
                                            "totalCount": 2,
                                            "totalPages": 1
                                        }
                                    }""")))
    })
    public Result myOrdersFind(@RequestBody MyOrdersQueryDto myOrdersQueryDto) {
        return ordersService.myOrdersFind(myOrdersQueryDto);
    }


    @PostMapping("/add/image")
    @Operation(summary = "图片上传接口", description = "上传图片获取取件码")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "上传成功"),
            @ApiResponse(responseCode = "400", description = "上传失败")
    })
    public Result addOrderImage(@RequestBody MultipartFile image) throws IOException, TesseractException {
        return ordersService.addOrderImage(image);
    }

    @PostMapping("/add/order")
    @Operation(summary = "领取订单接口", description = "从任务大厅领取订单")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "领取成功",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 200,
                                        "msg": "success",
                                        "data": null
                                    }"""))),
            @ApiResponse(responseCode = "400", description = "领取失败",
                    content = @Content(
                            mediaType = "application/json",
                            schema = @Schema(implementation = Result.class),
                            examples = @ExampleObject(value = """
                                    {
                                        "code": 400,
                                        "msg": "领取失败",
                                        "data": null
                                    }""")))
    })
    public Result getOrder(@RequestHeader("Authorization") String token, @RequestParam("orderId") Integer orderId) {
        Integer userId = jwtUtil.getUserIdFromToken(token);
        if (ordersService.getOrder(userId, orderId)) {
            return Result.success(null);
        } else
            return new Result(400, "领取失败", null);


    }


}
